package org.jeecg.modules.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListUtils {

    public static <T> Map returnMap(List<T> list, int pageIndex, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        List<T> list1 = new ArrayList<>();
        if (list != null && list.size() > 0) {
            list1 = ListUtils.convert(list, pageIndex, pageSize);
            map.put("data", list1);
            map.put("allCount", list.size());
        } else {
            map.put("data", null);
            map.put("allCount", 0);
        }
        return map;
    }

    public static <T> List<T> convert(List<T> allDetails, int pageIndex, int pageSize) {

        Pagefenye<T> pageInfo = new Pagefenye<>(allDetails.size(), pageIndex, pageSize, 5);

        //判断是否是最后一页,如果最后一页就显示最后一页的几条，不是最后一页就显示pageSize条
        if (pageIndex == pageInfo.getTotalPage())
            pageInfo.setList(allDetails.subList((pageIndex - 1) * pageSize, allDetails.size()));
        else {
            pageInfo.setList(allDetails.subList((pageIndex - 1) * pageSize, (pageIndex - 1) * pageSize + pageSize));
        }
        List<T> list = pageInfo.getList();
        return list;
    }
}
